/********************************************************************************
*                               libemu
*
*                    - x86 shellcode emulation -
*
*
* Copyright (C) 2007  Paul Baecher & Markus Koetter
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
* 
* 
*             contact nepenthesdev@users.sourceforge.net  
*
*******************************************************************************/

#ifndef HAVE_EMU_MEMORY_H
#define HAVE_EMU_MEMORY_H

#include <inttypes.h>
#include <emu.h>

void* bcopy (void* src, void* dest, unsigned int len);


#define FS_SEGMENT_DEFAULT_OFFSET 0x7ffdf000

inline long emu_memory_read_byte( emu_cpu * c, uint32_t addr, uint8_t *byte)
{
	return c->mem->access_byte( c, addr, mem_read, byte );
}

inline long emu_memory_read_word( emu_cpu * c, uint32_t addr,  uint16_t *word)
{
	return c->mem->access_word( c, addr, mem_read, word );
}

inline long emu_memory_read_dword( emu_cpu * c, uint32_t addr,  uint32_t *dword)
{
	return c->mem->access_dword( c, addr, mem_read, dword );
}

inline long emu_memory_read_block( emu_cpu * c, uint32_t addr,  void *dest, size_t len)
{
	return c->mem->access_bytes( c, addr, mem_read, dest, len, 0 );
}

inline long emu_memory_write_byte( emu_cpu * c, uint32_t addr,  uint8_t byte)
{
	return c->mem->access_byte( c, addr, mem_write, &byte );
}

inline long emu_memory_write_word( emu_cpu * c, uint32_t addr,  uint16_t word)
{
	return c->mem->access_word( c, addr, mem_write, &word );
}

inline long emu_memory_write_dword( emu_cpu * c, uint32_t addr,  uint32_t dword)
{
	return c->mem->access_dword( c, addr, mem_write, &dword );
}

inline long emu_memory_write_block( emu_cpu * c, uint32_t addr,  void *src, size_t len)
{
	return c->mem->access_bytes( c, addr, mem_write, &src, len, 0 );
}



#define MEM_BYTE_READ(cpu_p, addr, data_p) \
{ int32_t ret = emu_memory_read_byte((cpu_p), addr, data_p); \
	if( ret != 0 ) \
	return ret; }

#define MEM_BYTE_WRITE(cpu_p, addr, data) \
{ int32_t ret = emu_memory_write_byte((cpu_p), addr, data); \
	if( ret != 0 ) \
	return ret; }

#define MEM_WORD_READ(cpu_p, addr, data_p) \
{ int32_t ret = emu_memory_read_word((cpu_p), addr, data_p); \
	if( ret != 0 ) \
	return ret; }

#define MEM_WORD_WRITE(cpu_p, addr, data) \
{ uint16_t val; \
	bcopy(&(data), &val, 2); \
	int32_t ret = emu_memory_write_word((cpu_p), addr, val); \
	if( ret != 0 ) \
	return ret; }

#define MEM_DWORD_READ(cpu_p, addr, data_p) \
{ int32_t ret = emu_memory_read_dword((cpu_p), addr, data_p); \
	if( ret != 0 ) \
	return ret; }

#define MEM_DWORD_WRITE(cpu_p, addr, data) \
{ uint32_t val; \
	bcopy(&(data), &val, 4); \
	int32_t ret = emu_memory_write_dword((cpu_p), addr, val); \
	if( ret != 0 ) \
	return ret; }


#endif // HAVE_EMU_MEMORY_H
